//
//  AppDelegate.m
//  youkan
//
//  Created by mac mini on 16/2/24.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "AppDelegate.h"
//#import "YKTabBarController.h"
//#import "YKNewFeatureController.h"
#import "YKOAuthViewController.h"
#import "YKAccountTool.h"
#import "YKRootTool.h"
#import "UIImageView+WebCache.h"
#import <AVFoundation/AVFoundation.h>
#import "AFNetworkActivityIndicatorManager.h"
@interface AppDelegate ()
@property(nonatomic,strong) AVAudioPlayer *player;
@end

@implementation AppDelegate
//偏好设置的好处  不需要文件名， 快速通过键值对赋值
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:networkActivityIndicatorVisible];
//    [application setNetworkActivityIndicatorVisible:true];
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

//    注册通知（后台刷新数据）
    UIUserNotificationSettings *setting =[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [application registerUserNotificationSettings:setting];
    
//    在真机上后台播放，设置音频会话
    AVAudioSession *session = [AVAudioSession sharedInstance];
//    设置会话类型（后台播放）
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
//     激活会话
    [session setActive:YES error:nil];
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor =[UIColor yellowColor];
    //选择根控制器，先判断有没有授权 ,进行授权
    if ([YKAccountTool account]) {
        [YKRootTool chooseRootViewController:self.window];
    }else{
        YKOAuthViewController *oauthVC=[[YKOAuthViewController alloc]init];
        self.window.rootViewController =oauthVC;
    }

//    NSLog(@"%@",application.keyWindow);
//    显示窗口相当于    self.window.hidden = NO;
    [self.window makeKeyAndVisible]; ///
    NSLog(@"kSCHeight = %f",SCHeight);
//    if (@available(iOS 11.0, *)) {
//        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIApplicationBackgroundFetchIntervalNever];
//        [[UITableView appearance] setEstimatedRowHeight:0.f];
//        [[UITableView appearance] setEstimatedSectionHeaderHeight:0.f];
//        [[UITableView appearance] setEstimatedSectionFooterHeight:0.f];
//    }
    
    return YES;
}

//选择根控制器
/*
-(void)chooseRootViewController{

    NSString *currentVersinon = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSString *lastVersion= [[NSUserDefaults standardUserDefaults]objectForKey:YKVersionKey];
    if ([currentVersinon isEqualToString:lastVersion]) {
        YKTabBarController *tabBarVc=[[YKTabBarController alloc]init];
        tabBarVc.view.backgroundColor =[UIColor redColor];
        //            设置为根控制器
        self.window.rootViewController=tabBarVc;
    }else{
        YKNewFeatureController *newFeature = [[YKNewFeatureController alloc]init];
        self.window.rootViewController = newFeature;
        [[NSUserDefaults standardUserDefaults] setObject:currentVersinon forKey:YKVersionKey];
    }
    

}*/

//失去焦点
- (void)applicationWillResignActive:(UIApplication *)application {
//silence    爸比我要喝奶奶
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"silence.mp3" withExtension:nil];
    AVAudioPlayer *player =[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    [player prepareToPlay];
    player.numberOfLoops = -1;
    [player play];
    _player =player;
}
//      程序进入到后台的时候调用
- (void)applicationDidEnterBackground:(UIApplication *)application {
//    开启后台任务 时间不确定 优先级比较低，系统要关闭，优先考虑这个
    UIBackgroundTaskIdentifier ID =[application beginBackgroundTaskWithExpirationHandler:^{
//      后台任务结束时调用
        [ application endBackgroundTask:ID];
    }];
//告诉苹果后台在播放程序（音乐）可以提高优先级，但是苹果会实时监测是否在播放音乐
//    微博，在程序将要进入到后台的时候播放音乐（静音的音乐），3KB
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
//      程序收到内存经过后做出操作
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    [[SDWebImageManager sharedManager]cancelAll];
    [[SDWebImageManager sharedManager].imageCache clearMemory];
}
@end
